package com.streamsets.datacollector.pileline.stage;

import com.streamsets.datacollector.pileline.enums.HiveQueryConfigEnum;
import com.streamsets.datacollector.restapi.bean.ConfigConfigurationJson;
import com.streamsets.datacollector.restapi.bean.StageConfigurationJson;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 初始化hive查询配置
 * @author wengzhonghui
 * @date 2019/10/16 19:12
 */
public class InitHiveQueryConfig {

    /*
     * hive query stage名称
     */
    public static final String STAGE_HIVE_QUERY_NAME = "HiveQuery_01";

    public static void init(StageConfigurationJson stage, String defaultHiveJdbcUrl, String defaultHiveUsername, String defaultHivePassword){
        if(STAGE_HIVE_QUERY_NAME.equalsIgnoreCase(stage.getInstanceName())) {
            if (StringUtils.isNotBlank(defaultHiveJdbcUrl)) {
                List<ConfigConfigurationJson> configs = intConfig(stage.getConfiguration(), defaultHiveJdbcUrl, defaultHiveUsername, defaultHivePassword);
                stage.setConfiguration(configs);
            }
        }
    }

    public static List<ConfigConfigurationJson> intConfig(List<ConfigConfigurationJson> oldConfigs, String defaultHiveJdbcUrl, String defaultHiveUsername, String defaultHivePassword){
        List<ConfigConfigurationJson> newConfigs = new ArrayList<>();
        for(ConfigConfigurationJson config :oldConfigs ){
            config = initHiveJDBCUrl(config, defaultHiveJdbcUrl);
            config = initHiveJDBCUsername(config, defaultHiveUsername);
            config = initHiveJDBCPassword(config, defaultHivePassword);
            config = initHiveDriver(config);
            newConfigs.add(config);
        }
        return newConfigs;
    }


    // 初始化hiveJdbc连接
    private static ConfigConfigurationJson initHiveJDBCUrl(ConfigConfigurationJson config, String defaultHiveJdbcUrl){
        if(HiveQueryConfigEnum.hiveJDBCUrl.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(HiveQueryConfigEnum.hiveJDBCUrl.getName(),defaultHiveJdbcUrl);
        }
        return config;
    }
    private static ConfigConfigurationJson initHiveJDBCUsername(ConfigConfigurationJson config, String defaultHiveUsername){
        if(HiveQueryConfigEnum.hiveJDBCUsername.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(HiveQueryConfigEnum.hiveJDBCUsername.getName(),defaultHiveUsername);
        }
        return config;
    }
    private static ConfigConfigurationJson initHiveJDBCPassword(ConfigConfigurationJson config, String defaultHivePassword){
        if(HiveQueryConfigEnum.hiveJDBCPassword.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(HiveQueryConfigEnum.hiveJDBCPassword.getName(),defaultHivePassword);
        }
        return config;
    }

    // 初始化hive驱动
    private static ConfigConfigurationJson initHiveDriver(ConfigConfigurationJson config){
        if(HiveQueryConfigEnum.hiveJDBCDriver.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(HiveQueryConfigEnum.hiveJDBCDriver.getName()
                    ,HiveQueryConfigEnum.hiveJDBCDriver.getDefaultValue());
        }
        return config;
    }

}
